﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO

Namespace DB

    ''' <summary>
    ''' TypeName:    LicenseType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: SYS_License
    ''' </summary>
    ''' <remarks></remarks>

    <Serializable()> Public Class LicenseType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iLicenseId As Guid
        Protected Friend iCompanyName As String
        Protected Friend iAddress As String
        Protected Friend iContacts As String
        Protected Friend iLogoBanner() As Byte
        <NonSerialized()> Protected Friend iLogoBannerImage As Image
        Protected Friend iLogoReport() As Byte
        <NonSerialized()> Protected Friend iLogoReportImage As Image
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.SYS_License)
        End Sub
        Public Sub New(ByVal LicenseId As Guid)
            MyBase.New(TableNamesEnum.SYS_License, LicenseId)
        End Sub
#End Region

#Region "Exposed Properties"
        Public Property LicenseId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property CompanyName() As String
            Get
                Return Me.iCompanyName
            End Get
            Set(ByVal value As String)
                Me.iCompanyName = value
            End Set
        End Property
        Public Property Address() As String
            Get
                Return Me.iAddress
            End Get
            Set(ByVal value As String)
                Me.iAddress = value
            End Set
        End Property
        Public Property Contacts() As String
            Get
                Return Me.iContacts
            End Get
            Set(ByVal value As String)
                Me.iContacts = value
            End Set
        End Property
        Public Property LogoBanner() As Byte()
            Get
                Return Me.iLogoBanner
            End Get
            Set(ByVal value As Byte())
                Me.iLogoBanner = value
            End Set
        End Property
        Public Property LogoBannerImage As Image
            Get
                If Me.iLogoBanner IsNot Nothing Then
                    If Me.iLogoBannerImage Is Nothing Then
                        Dim ms As New MemoryStream(Me.iLogoBanner)
                        Me.iLogoBannerImage = Image.FromStream(ms)
                        Return Me.iLogoBannerImage
                    Else
                        Return Me.iLogoBannerImage
                    End If
                Else
                    Return Nothing
                End If
            End Get
            Set(Value As Image)
                If Value IsNot Nothing Then
                    Dim ms As New MemoryStream
                    Me.iLogoBannerImage = Value
                    Me.iLogoBannerImage.Save(ms, Imaging.ImageFormat.Png)
                    Me.iLogoBanner = ms.GetBuffer
                Else
                    Me.iLogoBanner = Nothing
                    Me.iLogoBannerImage = Nothing
                End If
            End Set
        End Property
        Public Property LogoReport As Byte()
            Get
                Return Me.iLogoReport
            End Get
            Set(ByVal value As Byte())
                Me.iLogoReport = value
            End Set
        End Property
        Public Property LogoReportImage As Image
            Get
                If Me.iLogoReport IsNot Nothing Then
                    If Me.iLogoReportImage Is Nothing Then
                        Dim ms As New MemoryStream(Me.iLogoReport)
                        Me.iLogoReportImage = Image.FromStream(ms)
                        Return Me.iLogoReportImage
                    Else
                        Return Me.iLogoReportImage
                    End If
                Else
                    Return Nothing
                End If
            End Get
            Set(Value As Image)
                If Value IsNot Nothing Then
                    Dim ms As New MemoryStream
                    Me.iLogoReportImage = Value
                    Me.iLogoReportImage.Save(ms, Imaging.ImageFormat.Png)
                    Me.iLogoReport = ms.GetBuffer
                Else
                    Me.iLogoReport = Nothing
                    Me.iLogoReportImage = Nothing
                End If
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iCompanyName = Nothing Else Me.iCompanyName = Row(1)
            If IsDBNull(Row(2)) Then Me.iAddress = Nothing Else Me.iAddress = Row(2)
            If IsDBNull(Row(3)) Then Me.iContacts = Nothing Else Me.iContacts = Row(3)
            If IsDBNull(Row(4)) Then
                Me.iLogoBanner = Nothing
                Me.iLogoBannerImage = Nothing
            Else
                Me.iLogoBanner = Row(4)
                Dim ms As New MemoryStream(Me.iLogoBanner)
                Me.iLogoBannerImage = Image.FromStream(ms)
            End If
            If IsDBNull(Row(5)) Then
                Me.iLogoReport = Nothing
                Me.iLogoReportImage = Nothing
            Else
                Me.iLogoReport = Row(5)
                Dim ms As New MemoryStream(Me.iLogoReport)
                Me.iLogoReportImage = Image.FromStream(ms)
            End If
            If IsDBNull(Row(6)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(6)
            If IsDBNull(Row(7)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(7)
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .Add(New SqlParameter("@LicenseId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LicenseId", Me.iId))
                    End If
                    If IsNothing(Me.iCompanyName) Then
                        .Add(New SqlParameter("@CompanyName", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@CompanyName", Me.iCompanyName))
                    End If
                    If IsNothing(Me.iAddress) Then
                        .Add(New SqlParameter("@Address", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@Address", Me.iAddress))
                    End If
                    If IsNothing(Me.iContacts) Then
                        .Add(New SqlParameter("@Contacts", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@Contacts", Me.iContacts))
                    End If
                    If IsNothing(Me.iLogoBanner) Then
                        .Add(New SqlParameter("@LogoBanner", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LogoBanner", Me.iLogoBanner))
                    End If
                    If IsNothing(Me.iLogoReport) Then
                        .Add(New SqlParameter("@LogoReport", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LogoReport", Me.iLogoReport))
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .Add(New SqlParameter("@LMUserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMUserId", Me.iLMUserId))
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .Add(New SqlParameter("@LMDate", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMDate", Me.iLMDate))
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw New UnexpectedException("Unexpected error failed to Insert " & Me.iTableName.ToString, ex)
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .Add(New SqlParameter("@LicenseId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LicenseId", Me.iId))
                    End If
                    If IsNothing(Me.iCompanyName) Then
                        .Add(New SqlParameter("@CompanyName", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@CompanyName", Me.iCompanyName))
                    End If
                    If IsNothing(Me.iAddress) Then
                        .Add(New SqlParameter("@Address", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@Address", Me.iAddress))
                    End If
                    If IsNothing(Me.iContacts) Then
                        .Add(New SqlParameter("@Contacts", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@Contacts", Me.iContacts))
                    End If
                    If IsNothing(Me.iLogoBanner) Then
                        .Add(New SqlParameter("@LogoBanner", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LogoBanner", Me.iLogoBanner))
                    End If
                    If IsNothing(Me.iLogoReport) Then
                        .Add(New SqlParameter("@LogoReport", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LogoReport", Me.iLogoReport))
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .Add(New SqlParameter("@LMUserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMUserId", Me.iLMUserId))
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .Add(New SqlParameter("@LMDate", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMDate", Me.iLMDate))
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw New UnexpectedException("Unexpected error failed to Update " & Me.iTableName.ToString, ex)
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iId.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return Nothing
        End Function
#End Region

    End Class

    Public Class LicenseTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of LicenseTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of LicenseTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.SYS_License)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As LicenseType
            Get
                Return CType(Me.List.Item(Index), LicenseType)
            End Get
            Set(ByVal value As LicenseType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As LicenseType

            For Each row In dt.Rows
                typeObject = New LicenseType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As LicenseTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum LicenseTypeFields
        LicenseId
        CompanyName
        Address
        Contacts
        LMUserId
        LMDate
    End Enum
#End Region

End Namespace